//
//  CellForRadioPlayList.m
//  世界
//
//  Created by admin on 16/2/23.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForRadioPlayList.h"
#import "DataBaseForRadio.h"
#import "ModelForRadioCollect.h"


@interface CellForRadioPlayList ()

@property (nonatomic, retain) UIView *viewForSeparator;
@property (nonatomic, assign) BOOL isSelect;


@end

@implementation CellForRadioPlayList

#pragma mark 重写dealloc方法
- (void)dealloc
{
    [super dealloc];
    
    [_imgViewForAnimation release];
    
    [_lableForNumber release];
    
    [_lableForTitle release];
    
    [_lableForPtime release];
    
    [_viewForSeparator release];
    
    [_buttonForCollect release];
}




#pragma mark 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self !=  nil)
    {
        self.viewForSeparator = [[UIView alloc] init];
        
        self.viewForSeparator.backgroundColor = [UIColor lightGrayColor];
        
        [self.contentView addSubview:self.viewForSeparator];
        
        [_viewForSeparator release];
        
        
        self.imgViewForAnimation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0.tiff"]];
        
        [self.contentView addSubview:self.imgViewForAnimation];
        
        NSMutableArray *arrZombie = [NSMutableArray array];
        
        for (int i = 0; i < 10 ; i ++)
        {
            NSString *name = [NSString stringWithFormat:@"%d.tiff", i];
            
            UIImage *image = [UIImage imageNamed:name];
            
            [arrZombie addObject:image];
        }
        
        // 动画API
        self.imgViewForAnimation.animationImages = arrZombie;
        
        // 动画间隔时间
        self.imgViewForAnimation.animationDuration = 1.5;
        
        // 动画重复次数
        self.imgViewForAnimation.animationRepeatCount = 0;
        
        
        
        
        self.lableForNumber = [[UILabel alloc] init];
        
        self.lableForNumber.textAlignment = NSTextAlignmentCenter;
        
        [self.lableForNumber setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        
        self.lableForNumber.textColor = [UIColor orangeColor];
        
        self.lableForNumber.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.lableForNumber];
        
        [_lableForNumber release];
        
        
        
        self.lableForTitle = [[UILabel alloc] init];
        
        self.lableForTitle.numberOfLines = 0;
        
        [self.contentView addSubview:self.lableForTitle];
        
        [_lableForTitle release];
        
        
        
        self.lableForPtime = [[UILabel alloc] init];
        
        [self.contentView addSubview:self.lableForPtime];
        
        [_lableForPtime release];
        
        
        
        
        self.buttonForCollect = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [self.contentView addSubview:self.buttonForCollect];
        
        [self.buttonForCollect addTarget:self action:@selector(buttonForCollectAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [_buttonForCollect release];

        
    }
    return self;
}



- (void)buttonForCollectAction:(UIButton *)button
{
    [[DataBaseForRadio shareDataBaseHandle]open];
    
    [[DataBaseForRadio shareDataBaseHandle] createTable];
    
    ModelForRadioCollect *model3 = self.arrForCollect[self.rom];
    
    self.isSelect =  [[DataBaseForRadio shareDataBaseHandle]select:model3];
    
    if (self.isSelect)
    {
        [[DataBaseForRadio shareDataBaseHandle]deleteWithCode:model3.title];
        
        
        [self.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"取消收藏"] forState:UIControlStateNormal];
        
        self.isSelect = NO;
    
    }
    else
    {
        [[DataBaseForRadio shareDataBaseHandle] insertCollect:self.arrForCollect[self.rom]];
        
        [self.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateNormal];
        
        self.isSelect = YES;
    }

    
}




#pragma mark 布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.viewForSeparator.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 1);
    
    self.imgViewForAnimation.frame = CGRectMake(5, 20, 40, 40);
    
    self.imgViewForAnimation.layer.cornerRadius = 20;
    
    self.imgViewForAnimation.layer.masksToBounds = YES;
    
    self.imgViewForAnimation.layer.borderWidth = 1;
    
    self.imgViewForAnimation.layer.borderColor = [[UIColor orangeColor] CGColor];
    
    self.lableForNumber.frame = CGRectMake(5, 20, 40, 40);
    
    self.lableForNumber.layer.cornerRadius = 20;
    
    self.lableForNumber.layer.masksToBounds = YES;
    
    self.lableForNumber.layer.borderWidth = 2;
    
    self.lableForNumber.layer.borderColor = [[UIColor orangeColor] CGColor];
    
    self.lableForTitle.frame = CGRectMake(50, 5, self.contentView.frame.size.width - 45 - 60, 50);
    
    self.lableForPtime.frame = CGRectMake(50, 55, self.contentView.frame.size.width - 45 - 60, 20);
    
    self.buttonForCollect.frame = CGRectMake(360, 20, 40, 40);
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
